package com.xxx.demo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.demo.dto.GuestBookDto;
import com.xxx.demo.entity.GuestBook;
import com.xxx.demo.entity.News;
import com.xxx.demo.mapper.GuestBookMapper;
import com.xxx.demo.mapper.NewsMapper;
import com.xxx.demo.service.GuestBookService;
import com.xxx.demo.vo.GuestBookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author 562702
* @description 针对表【guest_book_table】的数据库操作Service实现
* @createDate 2023-03-27 16:16:10
*/
@Service
public class GuestBookServiceImpl extends ServiceImpl<GuestBookMapper, GuestBook>
    implements GuestBookService {

    @Autowired(required = false)
    private GuestBookMapper guestBookMapper;

    @Override
    public List<GuestBookVo> queryGuestBook() {
        List<GuestBookVo> guestBookVoList = guestBookMapper.selectGuestBook();
        if (CollectionUtil.isNotEmpty(guestBookVoList)){
            for(GuestBookVo guestBookVo : guestBookVoList){
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = simpleDateFormat.format(guestBookVo.getCreateTime());
                guestBookVo.setGuestBookTime(format);
            }
        }
        return guestBookVoList;
    }

    @Override
    public int saveGuestBook(GuestBook guestBook) {
        if (ObjectUtil.isEmpty(guestBook)){
            return 0;
        }
        guestBook.setCreateTime(new Date());
        int insert = guestBookMapper.insert(guestBook);
        return insert;
    }

    @Override
    public int removeGuestBookById(Long id) {
        return guestBookMapper.deleteById(id);
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @Override
    public int deleteGuestBook(Long id){
//        GuestBooke guestBook = this.queryGuestBookById(id);
        GuestBook guestBook = guestBookMapper.selectOne(new QueryWrapper<GuestBook>().eq("delete_flag", 0).eq("message_id", id));
        guestBook.setDeleteFlag(1);
        int i = guestBookMapper.updateById(guestBook);
        return i;
    }

    /**
     * 修改新闻
     * @param guestBook
     */
    @Override
    @Transactional
    public void updateGuestBook(GuestBook guestBook) {
        guestBookMapper.updateById(guestBook);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public GuestBook queryGuestBookById(Long id) {
        return guestBookMapper.selectById(id);
    }

    /**
     * 分页查询新闻
     * @param pageNo
     * @param pageSize
     * @param guestBookDto
     * @return
     */
    @Override
    public IPage<GuestBookVo> selectGuestBookPage(Integer pageNo, Integer pageSize, GuestBookDto guestBookDto){
        Page<GuestBookVo> guestBookIPage = new Page<>(pageNo,pageSize);
        return guestBookMapper.selectGuestBookPage(guestBookIPage,guestBookDto);
    }
}




